var MaterialCheckTypeModel = function (materialName, materialCheckTypeList) {
  BaseModel.call(this);
  var self = this;

  var fn = {
    initData: function (materialName, materialCheckTypeList) {
      materialCheckTypeList = _.map(materialCheckTypeList, function (materialCheck) {
        return {
          checkTypeId: materialCheck.checkTypeId,
          checkDesc: materialCheck.checkDesc
        };
      });
      
      self.materialName = materialName;
      self.materialCheckTypeList = materialCheckTypeList;
    }
  };

  this.addMaterial = function () {
    var materialCheck = {
      checkTypeId: '',
      checkDesc: ''
    };
    this.materialCheckTypeList.push(materialCheck);
  };

  this.checkValid = function () {
    var materialCheckTypeList = this.materialCheckTypeList;
    var count = materialCheckTypeList.length;
    if (count === 0) {
      return 'CheckTypeNull';
    }

    var checkTypeIds = [];
    for (var i = 0; i < count; i++) {
      var materialCheckType = materialCheckTypeList[i];
      if (materialCheckType.checkTypeId === '') {
        return ['checkTypeId', i];
      }
      checkTypeIds.push(materialCheckType.checkTypeId);
    }

    var ids = _.uniq(checkTypeIds);
    if (ids.length !== checkTypeIds.length) {
      return 'checkTypeRepeat';
    }

    return '';
  };

  this.getObject = function () {
    var postData = {
      materialName: this.materialName
    };

    var materialCheckTypeList = this.materialCheckTypeList;
    materialCheckTypeList = _.map(materialCheckTypeList, function (obj) {
      return {
        checkTypeId: obj.checkTypeId,
        checkDesc: obj.checkDesc
      };
    });
    postData.checkArray = JSON.stringify(materialCheckTypeList);

    return postData;
  };

  fn.initData(materialName, materialCheckTypeList);
}
